home *** CD-ROM | disk | FTP | other *** search
- #pragma segment Xfmath
-
- /*
- * fmath.c --
- *
- * Contains the TCL trig and floating point math functions.
- *---------------------------------------------------------------------------
- * Copyright 1991 Karl Lehenbauer and Mark Diekhans.
- *
- * Permission to use, copy, modify, and distribute this software and its
- * documentation for any purpose and without fee is hereby granted, provided
- * that the above copyright notice appear in all copies. Karl Lehenbauer and
- * Mark Diekhans make no representations about the suitability of this
- * software for any purpose. It is provided "as is" without express or
- * implied warranty.
- */
-
- #include <math.h>
- #include <sane.h>
- #include "tcl.h"
-
- #define FALSE 0
- #define TRUE 1
-
- #define CHECK_FP_ERROR() \
- ( testexception(INVALID | UNDERFLOW | OVERFLOW | DIVBYZERO) )
-
- /*
- * Flag used to indicate if a floating point math routine is currently being
- * execu-ed. Used to determine if a fmatherr belongs to Tcl.
- */
- static int G_inTclFPMath = FALSE;
-
- /*
- * Flag indicating if a floating point math error occured during the execution
- * of a library routine called by a Tcl command. Will not be set by the trap
- * handler if the error did not occur while the `G_inTclFPMath' flag was
- * set. If the error did occur the error type and the name of the function
- * that got the error are sa e here.
- */
- static int G_gotTclFPMathErr = FALSE;
- static char *G_functionName;
- static int G_errorType;
-
- /*
- * Prototypes of internal functions.
- */
- int
- Tcl_UnaryFloatFunction _ANSI_ARGS_((Tcl_Interp *interp,
- int argc,
- char **argv,
- double (*function)()));
-
-
- /*
- *----------------------------------------------------------------------
- *
- * ReturnFPMathError --
- * Routine to set an interpreter result to contain a floating point
- * math error message. Will clear the `G_gotTclFPMathErr' flag.
- * This routine al ays returns the value TCL_ERROR, so if can be called
- * as the argument to `return'.
- *
- * Globals:
- * o G_gotTclFPMathErr (O) - Flag indicating an error occured, will be
- * cleared.
- * o G_functionName (I) - Name of function that got the error.
- * o G_errorType (I) - Type of error that occured.
- *----------------------------------------------------------------------
- */
- static int
- ReturnFPMathError(interp)
- Tcl_Interp *interp;
- {
- char *ers;
-
- if (testexception(INVALID))
- ers = "INVALID";
- else if (testexception(UNDERFLOW))
- ers = "UNDERFLOW";
- else if (testexception(OVERFLOW))
- ers = "OVERFLOW";
- else if (testexception(DIVBYZERO))
- ers = "DIVBYZERO";
- else if (testexception(INEXACT))
- ers = "INEXACT";
-
- sprintf(interp->result, "%s: floating point %s error", G_functionName, ers);
-
- return TCL_ERROR;
- }
-
- /*
- *----------------------------------------------------------------------
- *
- * Tcl_UnaryFloatFunction --
- * Helper routine that implements Tcl unary floating point
- * functions by validating parameters, converting the
- * argument, applying the function (the address of which
- * is passed as an argument), and converting the result to
- * a string and storing it in the result buffer
- *
- * Results:
- * Returns TCL_OK if number is present, conversion succeeded,
- * the function was performed,